<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java内存溢出深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.5);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 16px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .icon-box {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.2) 0%, rgba(118, 75, 162, 0.2) 100%);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <i class="fas fa-memory text-6xl mb-6 floating-icon"></i>
                <h1 class="text-5xl font-bold mb-4">Java 内存溢出深度解析</h1>
                <p class="text-xl opacity-90 max-w-3xl mx-auto">
                    掌握内存管理的艺术，构建稳定高效的 Java 应用
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card p-8 mb-8">
            <div class="flex items-start space-x-4">
                <div class="icon-box">
                    <i class="fas fa-exclamation-triangle text-2xl gradient-text"></i>
                </div>
                <div class="flex-1">
                    <h2 class="text-2xl font-bold mb-4">什么是内存溢出？</h2>
                    <p class="text-gray-700 leading-relaxed text-lg">
                        内存溢出（OutOfMemoryError）是 Java 开发中最常见且最具挑战性的问题之一。当 JVM 无法分配足够的内存来满足程序需求时，就会抛出这个错误，导致程序崩溃。尽管 Java 提供了自动垃圾回收机制，但不正确的资源管理或配置仍会引发内存溢出问题。
                    </p>
                </div>
            </div>
        </div>

        <!-- Memory Overview Visualization -->
        <div class="content-card p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 section-title">JVM 内存结构概览</h2>
            <div class="mermaid">
                graph TB
                    JVM[JVM 内存结构]
                    JVM --> Heap[堆内存]
                    JVM --> NonHeap[非堆内存]
                    
                    Heap --> Young[年轻代]
                    Heap --> Old[老年代]
                    
                    Young --> Eden[Eden 区]
                    Young --> S0[Survivor 0]
                    Young --> S1[Survivor 1]
                    
                    NonHeap --> Method[方法区/元空间]
                    NonHeap --> Stack[线程栈]
                    NonHeap --> Direct[直接内存]
                    
                    style JVM fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style Heap fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style NonHeap fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Types of OOM -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
            <div class="card-hover content-card p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-database text-3xl gradient-text mr-4"></i>
                    <h3 class="text-xl font-bold">堆内存溢出</h3>
                </div>
                <p class="text-gray-700 mb-4">最常见的内存溢出类型，发生在对象创建过多而垃圾回收无法及时清理时。</p>
                <div class="code-block text-sm">
                    java.lang.OutOfMemoryError: Java heap space
                </div>
            </div>
            
            <div class="card-hover content-card p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-layer-group text-3xl gradient-text mr-4"></i>
                    <h3 class="text-xl font-bold">方法区/元空间溢出</h3>
                </div>
                <p class="text-gray-700 mb-4">类加载过多或动态代理生成大量类时发生，影响类的元数据存储。</p>
                <div class="code-block text-sm">
                    java.lang.OutOfMemoryError: Metaspace
                </div>
            </div>
            
            <div class="card-hover content-card p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-bars text-3xl gradient-text mr-4"></i>
                    <h3 class="text-xl font-bold">栈内存溢出</h3>
                </div>
                <p class="text-gray-700 mb-4">递归调用过深或线程创建过多时发生，每个线程都需要独立的栈空间。</p>
                <div class="code-block text-sm">
                    java.lang.StackOverflowError
                </div>
            </div>
            
            <div class="card-hover content-card p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-microchip text-3xl gradient-text mr-4"></i>
                    <h3 class="text-xl font-bold">直接内存溢出</h3>
                </div>
                <p class="text-gray-700 mb-4">使用 NIO 时分配的直接内存超出限制，不受 JVM 堆管理。</p>
                <div class="code-block text-sm">
                    java.lang.OutOfMemoryError: Direct buffer memory
                </div>
            </div>
        </div>

        <!-- Common Scenarios -->
        <div class="content-card p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 section-title">常见内存溢出场景</h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="highlight-box">
                    <i class="fas fa-bug text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">内存泄漏</h4>
                    <p class="text-gray-700">长时间持有对象引用，垃圾回收器无法回收</p>
                </div>
                <div class="highlight-box">
                    <i class="fas fa-infinity text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">无限递归</h4>
                    <p class="text-gray-700">递归调用没有正确的退出条件</p>
                </div>
                <div class="highlight-box">
                    <i class="fas fa-expand text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">大对象分配</h4>
                    <p class="text-gray-700">单次分配过大的数组或缓存对象</p>
                </div>
                <div class="highlight-box">
                    <i class="fas fa-list text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">集合类滥用</h4>
                    <p class="text-gray-700">集合中对象数目过多且未及时清理</p>
                </div>
                <div class="highlight-box">
                    <i class="fas fa-link text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">静态引用</h4>
                    <p class="text-gray-700">静态字段持有大量对象引用</p>
                </div>
                <div class="highlight-box">
                    <i class="fas fa-cog text-2xl gradient-text mb-3"></i>
                    <h4 class="font-bold mb-2">配置不当</h4>
                    <p class="text-gray-700">JVM 内存参数设置不合理</p>
                </div>
            </div>
        </div>

        <!-- Detection Methods -->
        <div class="content-card p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 section-title">内存溢出检测方法</h2>
            <div class="space-y-6">
                <div class="flex items-start space-x-4">
                    <div class="icon-box flex-shrink-0">
                        <i class="fas fa-terminal text-xl gradient-text"></i>
                    </div>
                    <div>
                        <h4 class="font-bold mb-2">JVM 参数配置</h4>
                        <p class="text-gray-700 mb-3">启用详细的垃圾回收日志和堆转储功能</p>
                        <div class="code-block">
-XX:+PrintGCDetails -XX:+PrintGCDateStamps
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/path/to/dump.hprof
                        </div>
                    </div>
                </div>
                
                <div class="flex items-start space-x-4">
                    <div class="icon-box flex-shrink-0">
                        <i class="fas fa-chart-line text-xl gradient-text"></i>
                    </div>
                    <div>
                        <h4 class="font-bold mb-2">监控工具</h4>
                        <p class="text-gray-700 mb-3">使用专业工具实时监控内存使用情况</p>
                        <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                            <div class="text-center p-3 bg-gray-100 rounded">
                                <i class="fas fa-eye text-2xl gradient-text mb-2"></i>
                                <p class="text-sm">VisualVM</p>
                            </div>
                            <div class="text-center p-3 bg-gray-100 rounded">
                                <i class="fas fa-microscope text-2xl gradient-text mb-2"></i>
                                <p class="text-sm">Eclipse MAT</p>
                            </div>
                            <div class="text-center p-3 bg-gray-100 rounded">